package com.itheima.mall.web.ums;


import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.ums.MenuService;
import com.itheima.mall.entity.domain.dto.MenuDto;
import com.itheima.mall.entity.domain.ums.Menu;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author qinglang
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/menu")
@CrossOrigin(origins = "*")
public class MenuController {
    @DubboReference
    private MenuService menuService;

    @PostMapping("/create")
    public Result create(@RequestBody Menu menu) {
        menuService.create(menu);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result delete(@PathVariable Integer id) {
        menuService.delete(id);
        return Result.success();
    }

    @GetMapping("/list/{parentId}")
    public Result list(@PathVariable Integer parentId, Integer pageNum, Integer pageSize) {
        PageResult list = menuService.list(parentId, pageNum, pageSize);
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result treeList() {
        List<MenuDto> menuDtos = menuService.treeList();
        return Result.success(menuDtos);
    }

    @PostMapping("/update/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Menu menu) {
        menuService.updateM(menu);
        return Result.success();
    }

    @PostMapping("/updateHidden/{id}")
    public Result updateHidden(@PathVariable Integer id, Integer hidden) {
        menuService.updateHidden(id, hidden);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result menu(@PathVariable Integer id) {
        Menu menu = menuService.menu(id);
        return Result.success(menu);
    }
}

